package cn.tedu.management.building.service.impl;

import cn.tedu.management.building.dao.repository.BuildingRepository;
import cn.tedu.management.building.pojo.dto.BuildingParam;
import cn.tedu.management.building.pojo.dto.BuildingQuery;
import cn.tedu.management.building.pojo.entity.Building;
import cn.tedu.management.building.pojo.vo.BuildingVO;
import cn.tedu.management.building.service.BuildingService;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    private BuildingRepository buildingRepository;

    @Override
    public List<BuildingVO> select(BuildingQuery buildingQuery) {
        QueryWrapper<Building> queryWrapper=getBuildingQueryWrapper(buildingQuery);
        List<Building> lists = buildingRepository.list(queryWrapper);
        return lists.stream().map(list -> {
            BuildingVO buildingVO = new BuildingVO();
            BeanUtils.copyProperties(list, buildingVO);
            return buildingVO;
        }).collect(Collectors.toList());
    }

    private QueryWrapper<Building> getBuildingQueryWrapper(BuildingQuery buildingQuery) {
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();

        // 动态设置模糊查询条件
        if (buildingQuery.getId() != null) {
            queryWrapper.eq("id", buildingQuery.getId());
        }
        if (buildingQuery.getCommunityId() != null) {
            queryWrapper.eq("community_id", buildingQuery.getCommunityId());
        }
        if (StringUtils.isNotBlank(buildingQuery.getName())) {
            queryWrapper.like("name", buildingQuery.getName());
        }
        if (StringUtils.isNotBlank(buildingQuery.getCode())) {
            queryWrapper.like("code", buildingQuery.getCode());
        }
        if (StringUtils.isNotBlank(buildingQuery.getType())) {
            queryWrapper.like("type", buildingQuery.getType());
        }
        if (buildingQuery.getLayers() != null) {
            queryWrapper.eq("layers", buildingQuery.getLayers());
        }
        if (buildingQuery.getHouses() != null) {
            queryWrapper.eq("houses", buildingQuery.getHouses());
        }
        if (buildingQuery.getUpdateTime() != null) {
            queryWrapper.eq("update_time", buildingQuery.getUpdateTime());
        }
        if (buildingQuery.getFireUserId() != null) {
            queryWrapper.eq("fire_user_id", buildingQuery.getFireUserId());
        }
        if (buildingQuery.getSafeUserId()!=null) {
            queryWrapper.like("safe_user_id", buildingQuery.getSafeUserId());
        }
        if (buildingQuery.getPropertyUserId()!=null) {
            queryWrapper.like("property_user_id", buildingQuery.getPropertyUserId());
        }
        return queryWrapper;
    }

    @Override
    public BuildingVO selectById(Long id) {
        Building building = buildingRepository.getById(id);
        BuildingVO buildingVO = new BuildingVO();
        BeanUtils.copyProperties(building, buildingVO);
        return buildingVO;
    }

    @Override
    public void addBuilding(BuildingParam buildingParam) {
        Building building=dto2Entity(buildingParam);
        building.setCreateTime(new Date());
        buildingRepository.save(building);
    }

    @Override
    public void delete(Long id) {
        buildingRepository.removeById(id);
    }

    @Override
    public void update(BuildingParam buildingParam) {
        UpdateWrapper<Building> updateWrapper = getBuildingUpdateWrapper(buildingParam);
        updateWrapper.set("update_time", new Date());
        buildingRepository.update(null,updateWrapper);
    }

    @NotNull
    private static UpdateWrapper<Building> getBuildingUpdateWrapper(BuildingParam buildingParam) {
        UpdateWrapper<Building> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id", buildingParam.getId());
        if(buildingParam.getCode()!=null&&!"".equals(buildingParam.getCode())){
            updateWrapper.set("code", buildingParam.getCode());
        }
        if (buildingParam.getCommunityId()!=null){
            updateWrapper.set("community_id", buildingParam.getCommunityId());
        }
        if(buildingParam.getDevices()!=null&&!"".equals(buildingParam.getDevices())){
            updateWrapper.set("devices", buildingParam.getDevices());
        }
        if(buildingParam.getFireUserId()!=null){
            updateWrapper.set("fire_user_id", buildingParam.getFireUserId());
        }
        if(buildingParam.getHouses()!=null){
            updateWrapper.set("houses", buildingParam.getHouses());
        }
        if(buildingParam.getLayers()!=null){
            updateWrapper.set("layers", buildingParam.getLayers());
        }
        if(buildingParam.getName()!=null&&!"".equals(buildingParam.getName())){
            updateWrapper.set("name", buildingParam.getName());
        }
        if(buildingParam.getPropertyUserId()!=null){
            updateWrapper.set("property_user_id", buildingParam.getPropertyUserId());
        }
        if (buildingParam.getSafeUserId()!=null){
            updateWrapper.set("safe_user_id", buildingParam.getSafeUserId());
        }
        if(buildingParam.getType()!=null&&!"".equals(buildingParam.getType())){
            updateWrapper.set("type", buildingParam.getType());
        }
        if(buildingParam.getRemark()!=null&&!"".equals(buildingParam.getRemark())){
            updateWrapper.set("remark", buildingParam.getRemark());
        }
        return updateWrapper;
    }

    private Building dto2Entity(Object buildingParam) {
        Building building=new Building();
        BeanUtils.copyProperties(buildingParam,building);
        return building;
    }
}
